package info.xmlprint.action.exp;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class BasicExportPage extends WizardPage {
	private Text pathText;
	private Composite container;
	private Button destinationBrowseButton;
	private IStructuredSelection selection;

	private String[] filterExtension = new String[] { "*.*" };
	private String exportDialogTitle = "";

	public void setFilterExtension(String[] filterExtension) {
		this.filterExtension = filterExtension;
	}

	public void setExportDialogTitle(String exportDialogText) {
		this.exportDialogTitle = exportDialogText;
	}

	public BasicExportPage(IStructuredSelection selection) {
		super("");
		this.selection = selection;
		if (selection == null || selection.getFirstElement() == null) {
			this.setErrorMessage("There are no resources currently selected for export.");
		}
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(3, false));

		Label label = new Label(container, SWT.NONE);
		label.setText("To file");

		pathText = new Text(container, SWT.BORDER | SWT.SINGLE);
		pathText.setText("");
		pathText.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (getErrorMessage() == null && !pathText.getText().isEmpty()) {
					System.out.println();
					setPageComplete(true);
				}
			}

		});
		pathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		destinationBrowseButton = new Button(container, SWT.NONE);
		destinationBrowseButton.setText("Browser...");
		destinationBrowseButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				FileDialog dialog = new FileDialog(getContainer().getShell(),
						SWT.SAVE | SWT.SHEET);
				dialog.setFilterExtensions(filterExtension); //$NON-NLS-1$ //$NON-NLS-2$
				dialog.setText(exportDialogTitle);
				String currentSourceString = pathText.getText();
				int lastSeparatorIndex = currentSourceString
						.lastIndexOf(File.separator);
				if (lastSeparatorIndex != -1) {
					dialog.setFilterPath(currentSourceString.substring(0,
							lastSeparatorIndex));
				}
				String selectedFileName = dialog.open();

				if (selectedFileName != null) {
					pathText.setText(selectedFileName);
					if (selection != null
							&& selection.getFirstElement() != null) {
						setPageComplete(true);
						setErrorMessage(null);
					}

				}
			}

		});

		setControl(container);
		setPageComplete(false);
	}

	public String getExportPath() {
		return pathText.getText().trim();
	}

	public boolean validExportFile() {
		String path = getExportPath();
		if (!path.isEmpty()) {
			File file = new File(path);
			if (file.exists()) {
				if (MessageDialog
						.openConfirm(null, "Question",
								"Target file already exists. Would you like to overwrite it?")) {
					setPageComplete(true);
					return true;

				} else {
					setPageComplete(false);
					return false;
				}
			}
			return true;
		}
		return false;
	}
}